import 'dart:typed_data';
import 'dart:convert';

class Util {
  //region 单例模式
  static Util? _instance;

  factory Util() => _getInstance()!;

  static Util? get instance => _getInstance();

  static Util? _getInstance() {
    _instance ??= Util._internal();
    return _instance;
  }

  Util._internal() {
    _init();
  }

  _init() {}

  //endregion

  ///Int 转 ByteArray
  List<int> toByteArray(int value) {
    return [
      (value >> 24) & 0xFF,
      (value >> 16) & 0xFF,
      (value >> 8) & 0xFF,
      value & 0xFF,
    ];
  }

  /// 返回字节数组的十六进制表示形式的字符串
  String toHexString(Uint8List bytes) {
    if (bytes.isEmpty) return "[]";
    StringBuffer b = StringBuffer();
    b.write('[');
    for (int i = 0; i < bytes.length; i++) {
      b.write(bytes[i].toRadixString(16).padLeft(2, '0'));
      if (i != bytes.length - 1) b.write(", ");
    }
    b.write(']');
    return b.toString();
  }

  /// short转换为字节数组
  Uint8List short2Bytes(int value) {
    Uint8List data = Uint8List(2);
    data[0] = (value >> 8) & 0xff;
    data[1] = value & 0xFF;
    return data;
  }

  /// 返回指定的32位有符号整数值的字节数组表示形式
  Uint8List getBytes(int data) {
    Uint8List bytes = Uint8List(4);
    if (isLittleEndian()) {
      bytes[0] = data & 0xff;
      bytes[1] = (data >> 8) & 0xff;
      bytes[2] = (data >> 16) & 0xff;
      bytes[3] = (data >> 24) & 0xff;
    } else {
      bytes[3] = data & 0xff;
      bytes[2] = (data >> 8) & 0xff;
      bytes[1] = (data >> 16) & 0xff;
      bytes[0] = (data >> 24) & 0xff;
    }
    return bytes;
  }

  /// 判断系统是否为小端序
  bool isLittleEndian() {
    var buffer = Uint8List(2);
    var view = ByteData.view(buffer.buffer);
    view.setInt16(0, 1, Endian.little);
    return buffer[0] == 1;
  }

  int toInt(List<int> byteArray, int start, int length) {
    List<int> subArray = byteArray.sublist(start, start + length);
    int result = 0;
    for (int i = 0; i < subArray.length; i++) {
      result = subArray[i] | (result << (8 * i));
    }
    return result;
  }

  int toLong(List<int> bytes, int start, int length) {
    List<int> subArray = bytes.sublist(start, start + length);
    int result = 0;
    for (int i = 0; i < subArray.length; i++) {
      result = (subArray[i] & 0xFF) | (result << 8);
    }
    return result;
  }

  int toUShort(List<int> byteArray, int start, int length) {
    List<int> subArray = byteArray.sublist(start, start + length);
    return (subArray[1].toUnsigned(8).toUnsigned(16)) |
        (subArray[0].toUnsigned(8).toUnsigned(16) * 256);
  }

  List<int> shortToBytes(int value) {
    List<int> data = List<int>.filled(2, 0);
    data[0] = (value >> 8) & 0xFF;
    data[1] = value & 0xFF;
    return data;
  }

  int byteArrayToLong(List<int> bytes, int start, int length) {
    List<int> subArray = bytes.sublist(start, start + length);
    int result = 0;
    for (int i = 0; i < subArray.length; i++) {
      result = (subArray[i] & 0xFF) | (result << 8);
    }
    return result;
  }

  int byteArrayToInt(List<int> bytes, int start, int length) {
    List<int> subArray = bytes.sublist(start, start + length);
    int result = 0;
    for (int i = 0; i < subArray.length; i++) {
      result = subArray[i] | (result << (8 * i));
    }
    return result;
  }

  String byteArrayToString(List<int> bytes, [String charsetName = "UTF-8"]) {
    try {
      return utf8.decode(bytes);
    } catch (e) {
      print("Error decoding bytes: $e");
      return ""; // 或者返回一个默认值
    }
  }

  int getIndexToLong(int value, int index) {
    return (value >> index) & 1;
  }

  /// 数组拷贝。
  ///
  /// @param src 字节数组。
  /// @param off 起始下标。
  /// @param len 拷贝长度。
  /// @return 指定长度的字节数组。
  List<int> copyFrom(List<int> src, int off, int len) {
    // return src.sublist(off, off + len);
    List<int> bits = List<int>.filled(len, 0);
    for (int i = off, j = 0; i < src.length && j < len; i++, j++) {
      bits[j] = src[i];
    }
    return bits;
  }
}
